package loadingFile;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class LoadingFile {
	
	private String pathFile;
	private Vector<KeyValuePair> dictionary;
	private Vector<KeyValuePair> data;
	
	public LoadingFile(String pathFile){
		this.dictionary = new Vector<KeyValuePair>();
		this.data = new Vector<KeyValuePair>();
		this.pathFile = pathFile;
		initDictionary();
	}
	
	public LoadingFile() {
		this.dictionary = new Vector<KeyValuePair>();
		initDictionary();
	}

	public Vector<KeyValuePair> loading(){
		File file = new File(this.pathFile);
	    FileInputStream fis = null;
	    BufferedInputStream bis = null;
	    BufferedReader dis = null;
	    try {
	      fis = new FileInputStream(file);
	      // Here BufferedInputStream is added for fast reading.
	      bis = new BufferedInputStream(fis);
	      dis = new BufferedReader(new InputStreamReader(fis));
	      String currWordInFile;
	      // dis.available() returns 0 if the file does not have more lines.
	      KeyValuePair objWord;
	      while ((currWordInFile = dis.readLine()) != null) {
	      // this statement reads the line from the file and print it to
	        // the console.
	      //  System.out.println(dis.readLine());
	    	  currWordInFile = currWordInFile.toLowerCase();
	    	  String seqNumber = findNumberFromWord(currWordInFile);

	    	  if(!seqNumber.contains("null") && seqNumber != null){
	    		  objWord = new KeyValuePair(seqNumber, currWordInFile);
		    	  dictionary.add(objWord);
	    	  }  
	      }

	      // dispose all the resources after using them.
	      fis.close();
	      bis.close();
	      dis.close();

	    } catch (FileNotFoundException e) {
	      e.printStackTrace();
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	    return this.dictionary;
	}
	
	
	public void initDictionary (){
		data.add(new KeyValuePair("a", 2));data.add(new KeyValuePair("d", 3));
		data.add(new KeyValuePair("b", 2));data.add(new KeyValuePair("e", 3));
		data.add(new KeyValuePair("c", 2));data.add(new KeyValuePair("f", 3));
		data.add(new KeyValuePair("g", 4));data.add(new KeyValuePair("j", 5));
		data.add(new KeyValuePair("h", 4));data.add(new KeyValuePair("k", 5));
		data.add(new KeyValuePair("i", 4));data.add(new KeyValuePair("l", 5));
		data.add(new KeyValuePair("m", 6));data.add(new KeyValuePair("t", 8));
		data.add(new KeyValuePair("n", 6));data.add(new KeyValuePair("u", 8));
		data.add(new KeyValuePair("o", 6));data.add(new KeyValuePair("v", 8));
		data.add(new KeyValuePair("p", 7));data.add(new KeyValuePair("w", 9));
		data.add(new KeyValuePair("q", 7));data.add(new KeyValuePair("x", 9));
		data.add(new KeyValuePair("r", 7));data.add(new KeyValuePair("y", 9));
		data.add(new KeyValuePair("s", 7));data.add(new KeyValuePair("z", 9));
	}
	
	public Vector<KeyValuePair> getDictionnary(){
		return data;
	}
	
	public String findNumberFromWord(String word){
		char[] arrLetters = word.toCharArray();
		String chainOfNumber = "";
		for(int i=0; i<word.length(); i++){
			chainOfNumber += getLetterInData(arrLetters[i]);
		}
		return chainOfNumber;
	}
	
	public Object getLetterInData (Object key) {
        Iterator<KeyValuePair> iterator = data.iterator ();
        while (iterator.hasNext ()) {
            KeyValuePair pair = (KeyValuePair) iterator.next ();
            //System.out.println("================================="+pair.getKey());
            //System.out.println("+++++++++++++++++++++++++++++++++"+key);
            if (key.toString().equals (pair.getKey())) {
                return pair.getValue();
            }
        }
        return null;
    }
	
	
	
	public ArrayList<KeyValuePair> search(String sequenceNumber){
		
		ArrayList<KeyValuePair> listResult = new ArrayList<KeyValuePair>();
		Iterator<KeyValuePair> iterator = dictionary.iterator ();
        while (iterator.hasNext ()) {
            KeyValuePair pair = (KeyValuePair) iterator.next ();
            //System.out.println("So " + sequenceNumber);
            //System.out.println("Key " + pair.getKey());
            if (sequenceNumber.equals (pair.getValue())) {
            	listResult.add(pair);
            }
        }
        return listResult;
	}
	
	
	//public String findTheNumberFrom(String word){
	//	for (char letter : word.toCharArray()) {
	//	}
	//}
}
